package br.grupecinc.conexaoBD;

import br.grupecinc.model.entity.Cadastro;
import br.grupecinc.model.entity.Mensalidade;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
 *
 * @author Kaynan
 */
public class SistemaDAO<T> {

    protected static EntityManagerFactory entityFactory;
    protected static EntityManager entityManager;

    static {
        entityFactory = Persistence.createEntityManagerFactory("ad-PU");
        entityManager = entityFactory.createEntityManager();
    }

    public T find(Class<T> entityClass, int id) {
        T result = (T) entityManager.find(entityClass, id);
        return result;
    }

    public List<T> find(String query) {
        return entityManager.createQuery(query).getResultList();
    }

    public T getReference(Class<T> entityClass, Long id) {
        return (T) entityManager.getReference(entityClass, id);
    }

    public static <K> K findNativeQuery(String query) {
        return (K) entityManager.createNativeQuery(query).getSingleResult();
    }

    public static <K> List<K> findNativeQueryList(String query) {
        return (List<K>) entityManager.createNativeQuery(query).getResultList();
    }

    public static Query findQueryParameterized(String query) {
        return entityManager.createQuery(query);
    }

    public static Query findNativeQueryParameterized(String query) {
        return entityManager.createNativeQuery(query);
    }

    public static Query createNamedQuery(String namedQuery) {
        return entityManager.createNamedQuery(namedQuery);
    }

    public static Query createQuery(String query) {
        return entityManager.createQuery(query);
    }
    
    public static void persistCadastro(Cadastro cadastro){
        entityManager.getTransaction().begin();
        entityManager.persist(cadastro);
        entityManager.getTransaction().commit();
    }

    public static void updateCadastro(Cadastro cadastro){
        entityManager.getTransaction().begin();
        entityManager.merge(cadastro);
        entityManager.getTransaction().commit();
    }
    
    public static void removeCadastro(Cadastro cadastro){
        entityManager.getTransaction().begin();
        entityManager.remove(cadastro);
        entityManager.getTransaction();
    }
    
    public static void persistMensalidade(Mensalidade mensalidade){
        entityManager.getTransaction().begin();
        entityManager.persist(mensalidade);
        entityManager.getTransaction().commit();
    }

    public static void updateMensalidade(Mensalidade mensalidade){
        entityManager.getTransaction().begin();
        entityManager.merge(mensalidade);
        entityManager.getTransaction().commit();
    }
    
    public static void removeMensalidade(Mensalidade mensalidade){
        entityManager.getTransaction().begin();
        entityManager.remove(mensalidade);
        entityManager.getTransaction();
    }
}
